﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using OneRed.Main.Comm;
using OneRed.File;
using System.Data;
using OneRed.Data;
using System.Threading.Tasks;
using System.Windows.Input;
using OneRed.Draw.LVCDraw;
using System.Collections.Generic;

namespace OneRed.Main.Pages
{
    /// <summary>
    /// Index.xaml 的交互逻辑
    /// </summary>
    public partial class Index : Page
    {
        public static Index StaticIndeX;
        private static string iSearchType = "sTitle";//默认检索项是标准
        public static Draw.LVCDraw.DrawBaseMap lvc;//绘制基本图形

        public Index()
        {
            InitializeComponent();
            StaticIndeX = this;
        }
        //显示或关闭参数设置页面
        bool blShow = false;
        private void VisHidParams(object sender, RoutedEventArgs e)
        {
            MainWindow.mw.VisHidParms(blShow);
            blShow = !blShow;
        }

        private void DeleteSelected(object sender, RoutedEventArgs e)
        {
            var vnum = FormatDataCollection.SelectedCount();

            if (FormatDataCollection.SelectedCount() < 1)
                return;
            FormatDataCollection.DeleteSelectedData();
            page.ShowPages(dgDataSource, FormatDataCollection.Results(), 10); //这里是调用设置分页的函数
        }

        //对数据进行检索
        private async void SearchClick(object sender, RoutedEventArgs e)
        {
            await Search(searchkey.Text);
        }
        private async void searchkey_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                await Search(searchkey.Text);
            }
        }
        //将检索的函数写在一个地方，其他按钮则调用此函数即可
        private async Task Search(string sSearchword)
        {
            Func<FormatData, bool> func;
            switch (iSearchType)
            {
                case "sTitle":
                    {
                        func = t => t.sTitle.Contains(sSearchword);
                        break;
                    }
                case "sKeywords":
                    {
                        func = t => t.sKeywords.Contains(sSearchword);
                        break;
                    }
                case "sAuthor":
                    {
                        func = t => t.sAuthor.Contains(sSearchword);
                        break;
                    }
                case "sAuthorOrgan":
                    {
                        func = t => t.sAuthorOrgan.Contains(sSearchword);
                        break;
                    }
                case "sJournal":
                    {
                        func = t => t.sJournal.Contains(sSearchword);
                        break;
                    }
                case "sSummary":
                    {
                        func = t => t.sSummary.Contains(sSearchword);
                        break;
                    }
                default:
                    {
                        func = t => t.sTitle.Contains(sSearchword);
                        break;
                    }
            }
            var vSearchReuslt = FormatDataCollection.Results().Where(func).ToList();
            if (vSearchReuslt.Count < 1)
                await MainWindow.MessegeShow("提示", "没有检索到相关数据");

            FormatDataCollection.addData(vSearchReuslt, true);
            page.ShowPages(dgDataSource, FormatDataCollection.Results(), 10); //这里是调用设置分页的函数
        }
        //检索类型发生改变时
        private void cbSearchType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //如果是初始化进行界面布局时不进行判断
            if (cbSearchType == null || searchkey == null)
                return;
            //获取检索的类型
            var index = cbSearchType.SelectedIndex;
            var CbType = sender as ComboBox;
            iSearchType = (CbType.SelectedItem as ComboBoxItem).Name;
            //await Search(searchkey.Text);
        }
        #region 绘制基本图形

        //ViewModel lvc = new ViewModel();
        private async void ButtonGroup_Click(object sender, RoutedEventArgs e)
        {
            if (FormatDataCollection.Count() < 1)
                await MainWindow.MessegeShow("提示", "请先导入数据再绘图");
            var btName = (e.Source as Button).Name;

            lvc = new Draw.LVCDraw.DrawBaseMap(btName);

            //MainWindow.slYears.Clear();
            //FileOprate.readYearsFromList(FormatDataCollection.Results(), MainWindow.slYears, MainWindow.slYearsPercent);//更新年度数据值
            if (!(bool)ParamsSet.StatitcParamSet.cbHaveTime.IsOn)
            {
                //无时间或没胡选择进行时间分析
                return;
            }
            if (MainWindow.slYears.Count < 1)
                return;
            //是起始时间及分片是否有变化，并根据选择项进行时间及相应数量的处理
            //SortedList<string, string> slSpYears = new SortedList<string, string>();//分片时间及起始时间
            MainWindow.slSpYears = FileOprate.SpliceYears(MainWindow.slYears, ParamsSet.StatitcParamSet.cbStartYear.Text, ParamsSet.StatitcParamSet.cbEndYear.Text, ParamsSet.StatitcParamSet.cbYearSplin.Text);

            if (MainWindow.slTitle.Count < 1)
                MainWindow.slTitle.Add("年度", "文档数量|作者数量");
            lvcBase.Visibility = lvcPie.Visibility = Visibility.Collapsed;
            if (btName == "basePie")
            {
                lvcPie.Visibility = Visibility.Visible;
            }
            else
            {
                lvcBase.Visibility = Visibility.Visible;
            }
            lvc.DrawMap(lvcBase, lvcPie, btName, MainWindow.slSpYears, MainWindow.slTitle);

        }

        #endregion
    }
}
